using WebRtcProject.Models.Configuration;

namespace WebRtcProject.Models.Messages
{
    /// <summary>
    /// WebRTC 信令消息
    /// 专为嵌入式设备语音通话优化
    /// </summary>
    public class WebRtcSignalingMessage : BaseMessage
    {
        /// <summary>
        /// 信令类型 (offer, answer, ice_candidate, call_start, call_end)
        /// </summary>
        public string SignalType { get; set; } = string.Empty;
        
        /// <summary>
        /// 源设备ID
        /// </summary>
        public string FromDeviceId { get; set; } = string.Empty;
        
        /// <summary>
        /// 目标设备ID
        /// </summary>
        public string ToDeviceId { get; set; } = string.Empty;
        
        /// <summary>
        /// 房间ID（可选）
        /// </summary>
        public string? RoomId { get; set; }
        
        /// <summary>
        /// 信令数据内容
        /// </summary>
        public object? Data { get; set; }
        
        /// <summary>
        /// 消息优先级
        /// </summary>
        public SignalingPriority Priority { get; set; } = SignalingPriority.Normal;
        
        /// <summary>
        /// 消息生存时间 (毫秒)
        /// </summary>
        public int TTL { get; set; } = 10000;
        
        /// <summary>
        /// 会话ID（用于关联同一通话的多个消息）
        /// </summary>
        public string? SessionId { get; set; }
        
        /// <summary>
        /// 设备能力信息
        /// </summary>
        public DeviceCapabilities? DeviceInfo { get; set; }
        
        /// <summary>
        /// 扩展元数据
        /// </summary>
        public Dictionary<string, object> Metadata { get; set; } = new();

        public WebRtcSignalingMessage()
        {
            MessageType = nameof(WebRtcSignalingMessage);
        }

        /// <summary>
        /// 创建 WebRTC Offer 消息
        /// </summary>
        public static WebRtcSignalingMessage CreateOffer(string fromDeviceId, string toDeviceId, 
            WebRtcOffer offer, string? sessionId = null, string? roomId = null)
        {
            return new WebRtcSignalingMessage
            {
                SignalType = "offer",
                FromDeviceId = fromDeviceId,
                ToDeviceId = toDeviceId,
                Data = offer,
                Priority = SignalingPriority.Critical,
                SessionId = sessionId ?? Guid.NewGuid().ToString(),
                RoomId = roomId,
                TTL = 10000 // 10秒过期
            };
        }

        /// <summary>
        /// 创建 WebRTC Answer 消息
        /// </summary>
        public static WebRtcSignalingMessage CreateAnswer(string fromDeviceId, string toDeviceId, 
            WebRtcAnswer answer, string sessionId, string? roomId = null)
        {
            return new WebRtcSignalingMessage
            {
                SignalType = "answer",
                FromDeviceId = fromDeviceId,
                ToDeviceId = toDeviceId,
                Data = answer,
                Priority = SignalingPriority.Critical,
                SessionId = sessionId,
                RoomId = roomId,
                TTL = 10000 // 10秒过期
            };
        }

        /// <summary>
        /// 创建 ICE Candidate 消息
        /// </summary>
        public static WebRtcSignalingMessage CreateIceCandidate(string fromDeviceId, string toDeviceId, 
            IceCandidate candidate, string sessionId, string? roomId = null)
        {
            return new WebRtcSignalingMessage
            {
                SignalType = "ice_candidate",
                FromDeviceId = fromDeviceId,
                ToDeviceId = toDeviceId,
                Data = candidate,
                Priority = SignalingPriority.High,
                SessionId = sessionId,
                RoomId = roomId,
                TTL = 15000 // 15秒过期
            };
        }

        /// <summary>
        /// 创建通话开始消息
        /// </summary>
        public static WebRtcSignalingMessage CreateCallStart(string fromDeviceId, string toDeviceId, 
            string sessionId, string? roomId = null)
        {
            return new WebRtcSignalingMessage
            {
                SignalType = "call_start",
                FromDeviceId = fromDeviceId,
                ToDeviceId = toDeviceId,
                Priority = SignalingPriority.Normal,
                SessionId = sessionId,
                RoomId = roomId,
                TTL = 30000 // 30秒过期
            };
        }

        /// <summary>
        /// 创建通话结束消息
        /// </summary>
        public static WebRtcSignalingMessage CreateCallEnd(string fromDeviceId, string toDeviceId, 
            string sessionId, string? roomId = null, string? reason = null)
        {
            return new WebRtcSignalingMessage
            {
                SignalType = "call_end",
                FromDeviceId = fromDeviceId,
                ToDeviceId = toDeviceId,
                Priority = SignalingPriority.Normal,
                SessionId = sessionId,
                RoomId = roomId,
                TTL = 60000, // 60秒过期
                Metadata = reason != null ? new Dictionary<string, object> { ["reason"] = reason } : new()
            };
        }
    }

    /// <summary>
    /// 信令消息优先级
    /// </summary>
    public enum SignalingPriority : byte
    {
        Low = 50,
        Normal = 100,
        High = 200,
        Critical = 255
    }

    /// <summary>
    /// 设备能力信息
    /// </summary>
    public class DeviceCapabilities
    {
        /// <summary>
        /// 设备类型
        /// </summary>
        public string DeviceType { get; set; } = string.Empty;
        
        /// <summary>
        /// 支持的音频编解码器
        /// </summary>
        public List<string> AudioCodecs { get; set; } = new();
        
        /// <summary>
        /// 支持的视频编解码器（如果有）
        /// </summary>
        public List<string> VideoCodecs { get; set; } = new();
        
        /// <summary>
        /// 最大并发通话数
        /// </summary>
        public int MaxConcurrentCalls { get; set; } = 1;
        
        /// <summary>
        /// 网络带宽限制 (kbps)
        /// </summary>
        public int BandwidthLimit { get; set; } = 64;
        
        /// <summary>
        /// 是否支持DTMF
        /// </summary>
        public bool SupportsDTMF { get; set; } = true;
        
        /// <summary>
        /// 设备固件版本
        /// </summary>
        public string? FirmwareVersion { get; set; }
        
        /// <summary>
        /// 扩展能力
        /// </summary>
        public Dictionary<string, object> Extensions { get; set; } = new();
    }

    /// <summary>
    /// 设备状态消息
    /// </summary>
    public class DeviceStatusMessage : BaseMessage
    {
        /// <summary>
        /// 设备ID
        /// </summary>
        public string DeviceId { get; set; } = string.Empty;
        
        /// <summary>
        /// 设备状态 (online, offline, busy, error)
        /// </summary>
        public string Status { get; set; } = string.Empty;
        
        /// <summary>
        /// 当前活跃通话数
        /// </summary>
        public int ActiveCalls { get; set; } = 0;
        
        /// <summary>
        /// 最后心跳时间
        /// </summary>
        public DateTime LastHeartbeat { get; set; } = DateTime.UtcNow;
        
        /// <summary>
        /// 设备能力信息
        /// </summary>
        public DeviceCapabilities? Capabilities { get; set; }
        
        /// <summary>
        /// 网络质量信息
        /// </summary>
        public NetworkQuality? NetworkInfo { get; set; }
        
        /// <summary>
        /// 扩展状态信息
        /// </summary>
        public Dictionary<string, object> ExtendedStatus { get; set; } = new();

        public DeviceStatusMessage()
        {
            MessageType = nameof(DeviceStatusMessage);
        }

        /// <summary>
        /// 创建设备上线消息
        /// </summary>
        public static DeviceStatusMessage CreateOnline(string deviceId, DeviceCapabilities capabilities)
        {
            return new DeviceStatusMessage
            {
                DeviceId = deviceId,
                Status = "online",
                Capabilities = capabilities,
                LastHeartbeat = DateTime.UtcNow
            };
        }

        /// <summary>
        /// 创建设备离线消息
        /// </summary>
        public static DeviceStatusMessage CreateOffline(string deviceId, string? reason = null)
        {
            var message = new DeviceStatusMessage
            {
                DeviceId = deviceId,
                Status = "offline",
                LastHeartbeat = DateTime.UtcNow
            };
            
            if (!string.IsNullOrEmpty(reason))
            {
                message.ExtendedStatus["offline_reason"] = reason;
            }
            
            return message;
        }
    }

    /// <summary>
    /// 网络质量信息
    /// </summary>
    public class NetworkQuality
    {
        /// <summary>
        /// 延迟 (毫秒)
        /// </summary>
        public int Latency { get; set; }
        
        /// <summary>
        /// 丢包率 (百分比)
        /// </summary>
        public double PacketLoss { get; set; }
        
        /// <summary>
        /// 带宽 (kbps)
        /// </summary>
        public int Bandwidth { get; set; }
        
        /// <summary>
        /// 抖动 (毫秒)
        /// </summary>
        public int Jitter { get; set; }
        
        /// <summary>
        /// 信号强度 (dBm)
        /// </summary>
        public int? SignalStrength { get; set; }
    }
} 